#include "StdAfx.h"
#include ".\dayrecord.h"

DayRecord::DayRecord( const COleDateTime& t,const CString& u )
:time(t)
,USN(u)
,str(NULL)
{
	str = new char[255];
	ZeroMemory(str,255);
}


DayRecord::~DayRecord(void)
{
	//AfxMessageBox(FormatRecord());
	if(str)
		delete str;
}
char* DayRecord::FormatRecord()
{
	DLNRecord hours[24];
	
	USN.GetBuffer();
	
	sprintf(str,"%s%02d%02d%02d",
		USN.GetBuffer(),
		time.GetYear()-1911,
		time.GetMonth(),
		time.GetDay());

	char szValue[7];
	for(int i=0;i<24;i++)
	{
		
		if(hours[i].value == 0 )
			strcat(str,"      ");
		else
		{
			ZeroMemory(szValue,7);
			sprintf(szValue,"%6.2f",hours[i].value);
			strcat(str,szValue);
		}
	}
	return str;
}

void DayRecord::Parse( char* line )
{
	ASSERT( line );
	// sizeof(id)+sizeof(date)+24*sizeof(value) ---> 8+6+24*6=158
	if( strlen(line)==0)
		return;
	char* p = line;

	//parse id
	//ZeroMemory(id,9);
	//strncpy(id,p,8);
	p += 8;

	char szYear[3];
	ZeroMemory(szYear,3);
	strncpy(szYear,p,2);
	int year = atoi(szYear)+1911;
	p += 2;

	char  szMonth[3];
	ZeroMemory(szMonth,3);
	strncpy(szMonth,p,2);
	int month = atoi(szMonth);
	p += 2;

	char szDay[3];
	ZeroMemory(szDay,3);
	strncpy(szDay,p,2);
	int day = atoi(szDay);
	p += 2;
	
	time.SetDate(year,month,day);

	char szValue[7];
	COleDateTime t;
	for(int i=0;i<24;i++)
	{
		ZeroMemory(szValue,7);
		strncpy(szValue,p,6);
		p += 6;

		t.SetDateTime(year,month,day,i,0,0);
		
		hours[i].time = t;
		hours[i].value= atof(szValue);
		
	}
}